﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyMapping.Core
{
    public class PersistenceManager
    {
        public static T GetObject<T>(string procedureName, ParameterProperty[] arr)
                    where T : new()
        {
            IStoreProcedure sp = default(IStoreProcedure);
            if (ModelConfigLoaderFactory.GetModelConfigLoader().TryLoadData<T>(procedureName, arr, out sp))
            {
                T m = ModelManager<T>.GetObject(sp);
                return m;
            }
            return default(T);
        }


        public static List<T> GetList<T>(string procedureName, ParameterProperty[] arr)
                    where T : new()
        {
            IStoreProcedure sp = default(IStoreProcedure);
            if (ModelConfigLoaderFactory.GetModelConfigLoader().TryLoadData<T>(procedureName, arr, out sp))
            {
                List<T> list = ModelManager<T>.GetList(sp);
                return list;
            }
            return default(List<T>);
        }

        public static bool  SaveObject<T>(string procedureName, T obj)
                    where T : new()
        {
            IStoreProcedure sp = default(IStoreProcedure);
            if (ModelConfigLoaderFactory.GetModelConfigLoader().TryLoadData<T>(procedureName, obj, out sp))
            {
                bool flag = ModelManager<T>.SaveObject(obj, sp);
                return flag;
            }
            return false;
        }

        public static bool UpdateObject<T>(string procedureName, T obj)
                                where T : new()
        {
            IStoreProcedure sp = default(IStoreProcedure);
            if (ModelConfigLoaderFactory.GetModelConfigLoader().TryLoadData<T>(procedureName, obj, out sp))
            {
                bool flag = ModelManager<T>.UpdateObject(obj, sp);
                return flag;
            }
            return false;
        }

        public static bool DeleteObject<T>(string procedureName, T obj)
            where T : new()
        {
            IStoreProcedure sp = default(IStoreProcedure);
            if (ModelConfigLoaderFactory.GetModelConfigLoader().TryLoadData<T>(procedureName, obj, out sp))
            {
                bool flag = ModelManager<T>.DeleteObject(obj, sp);
                return flag;
            }
            return false;       
        }

    }
}
